<html xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"  
      xmlns:c="http://java.sun.com/jsp/jstl/core">  

    <ui:composition template="layout.xhtml">
        <ui:define name="corpo">

            <h:form>
                <div id="breadcrumb">
                    <p:commandLink  actionListener="#{managerIndex.redirect('index.xhtml')}" value="Inicio" update="@form" immediate="true"/> >
                    <p:commandLink  actionListener="#{managerIndex.redirect('indexProtocolo.xhtml')}" value="Protocolo" update="@form" immediate="true"/> >
                    <p:commandLink  actionListener="#{managerIndex.redirect('buscarProtocolo.xhtml')}" value="Pesquisar" update="@form" immediate="true"/> > 
                    <p:commandLink value="Visualizar" update="@form" immediate="true"/>
                </div>
            </h:form>
            <p:tabView dynamic="true" id="tabView">  
                <p:tab id="tab1" title="Visualizar Protocolo">                  
                    <h:form id="orcamento" acceptcharset="utf-8"> 

                        <p:messages id="msg"/>

                        <fieldset>
                            <legend>Dados</legend> 
                            <h:panelGrid columns="4" id="panel"  styleClass="textAlignRight" columnClasses="textAlignRight"> 

                                <h:outputLabel value="Código:"/>
                                <h:outputText value="#{managerVisualizarProtocolo.protocolo.id} "/>
                                
                                <h:outputLabel value=""/>
                                <h:outputLabel value=""/>
                                
                                
                                <h:outputLabel value="Interessado:"/>
                                <h:outputText value="#{managerVisualizarProtocolo.pessoa.nome} "/>

                                <h:outputLabel value="Doc.:"/>
                                <h:outputText value="#{managerVisualizarProtocolo.pessoa.cpf eq null ? managerVisualizarProtocolo.pessoa.cnpj : managerVisualizarProtocolo.pessoa.cpf} "/>

                            </h:panelGrid>
                            <h:panelGrid columns="2" styleClass="textAlignRight" columnClasses="textAlignRight"> 
                                <h:outputLabel value="Estado Civil:"/>
                                <h:outputText value="#{managerVisualizarProtocolo.pessoa.estadocivil eq null ? ' - ' : managerVisualizarProtocolo.pessoa.estadocivil} "/>

                                <h:outputLabel value="Profissão:"/>
                                <h:outputText value="#{managerVisualizarProtocolo.pessoa.profissao eq null ?  ' - ' : managerVisualizarProtocolo.pessoa.profissao} "/>
                            </h:panelGrid>



                            <br/>
                        </fieldset>
                        <fieldset>
                            <legend>Serviço</legend> 
                            <h:panelGrid id="dataTableAtoItem">
                                <p:dataTable var="serv"  
                                             value="#{managerVisualizarProtocolo.protocolo.servicoProtocolos}"
                                             rowIndexVar="rowid" style="width:930px;" 
                                             rows="8"
                                             rowKey="#{serv.id}" 
                                             emptyMessage="Nenhum Serviço Cadastrado" 
                                             paginatorPosition="bottom" paginator="true"
                                             paginatorTemplate="{PageLinks}"  
                                             >  
                                    <f:facet name="header">
                                        Serviços Protocolo
                                    </f:facet>
                                    <p:column headerText="" style="width: 25px">  
                                        <p:commandButton  style="width: 20px; height: 20px;" onclick="showProtocoloAto({paramShowProAtoVisu:#{rowid}})" 
                                                          icon="ui-icon-search" process="@this" title="Visualizar"/>

                                    </p:column>
                                    <p:column headerText="Ato"  >  
                                        <h:outputText value="#{serv.atoItem.codigo} - #{serv.atoItem.descricao}" />  
                                    </p:column>
                                    <p:column headerText="Livro"  >  
                                        <h:outputText value="#{serv.livro.titulo}" />  
                                    </p:column>
                                    <p:column headerText="Folha" style="width: 40px;"> 
                                           <h:outputLabel value="#{serv.folhasString}"/>
                                    </p:column>  
                                    <p:column headerText="Prazo" style="width:40px">  
                                        <h:outputText value="#{serv.atoItem.prazo}" style="float: right;" />  
                                    </p:column>
                                    <p:column headerText="Cartório" >  
                                        <h:outputText value="#{serv.valorCartorio}" style="float: right;" > 
                                            <f:convertNumber pattern="#,###,##0.00"/>
                                        </h:outputText>
                                    </p:column>
                                    <p:column headerText="Fermojupi" >  
                                        <h:outputText value="#{serv.valorFermojupi}" style="float: right;" > 
                                            <f:convertNumber pattern="#,###,##0.00"/>
                                        </h:outputText>
                                    </p:column>
                                    <p:column headerText="Valor R$" >  
                                        <h:outputText value="#{serv.valor}" styleClass="teste" style="float: right;" >
                                            <f:convertNumber pattern="#,###,##0.00"/>
                                        </h:outputText>
                                    </p:column>
                                    <p:column headerText="Selos" >  
                                        <h:outputLabel value="SEM SELOS" rendered="#{empty serv.selos}"/>
                                        <ui:repeat value="#{serv.selos}" var="sel">
                                            <p:commandLink  id="selos" title="Número: #{sel.numero} - Serie: #{sel.serie}" 
                                                            actionListener="#{managerVisualizarProtocolo.protocoloGetSelos(sel.id)}"
                                                            oncomplete="seloDlg.show()" update=":tabView:orcamento:pesquisarProtocoloDlg"  >
                                                <div style="width: 30px; height:30px; float:left; margin-left: 2px;
                                                     background-color: \##{sel.tipoSelo.corSelo.cor};">

                                                </div>
                                            </p:commandLink>
                                        </ui:repeat>
                                    </p:column>
                                </p:dataTable>

                            </h:panelGrid>
                            <p:remoteCommand name="showProtocoloAto"
                                             update="msg, atoItemTable, servico, panelCamposVisualizar" 
                                             immediate="true" process="@this" oncomplete="servicoAtoProt.show()" 
                                             actionListener="#{managerVisualizarProtocolo.showDialog()}">
                            </p:remoteCommand>
                            <p:dialog id="servicoAto" modal="true" header="Serviço" widgetVar="servicoAtoProt" resizable="false"  
                                      width="450" minHeight="400" showEffect="slide" hideEffect="fold" >

                                <h:panelGrid id="servico"   columns="2" styleClass="textAlignRight" columnClasses="textAlignRight">

                                    <h:outputLabel value="Cód.: " style="font-weight: bold;"/>
                                    <h:outputText value="#{managerVisualizarProtocolo.spVisualizar.atoItem.id}"/>
                                    <h:outputLabel value="Descrição.: " style="font-weight: bold;"/>
                                    <h:outputText value="#{managerVisualizarProtocolo.spVisualizar.atoItem.descricao}"/>



                                </h:panelGrid>
                                <h:panelGrid columns="1"  id="panelCamposVisualizar" > 
                                    <table >
                                        <ui:repeat id="rptCamposVisualizar" value="#{managerVisualizarProtocolo.spVisualizar.protocoloCampos}" 
                                                   rendered="#{! empty managerVisualizarProtocolo.spVisualizar.protocoloCampos}" 
                                                   var="orcVer" >
                                            <tr >
                                                <td class="textAlignRight" style="width:29px; margin-left: 0px" >
                                                    <h:outputLabel  value="#{orcVer.atoCampo.nome}: " style="font-weight: bold;"/> 
                                                </td>
                                                <td>
                                                    <h:outputText  value="#{orcVer.valor}" rendered="#{orcVer.atoCampo.tipo.equalsIgnoreCase('Integer')}" > 
                                                        <f:convertNumber pattern="#,###,##0.00"/>
                                                    </h:outputText>
                                                    <h:outputText  value="#{orcVer.valor}" rendered="#{orcVer.atoCampo.tipo.equalsIgnoreCase('BigDecimal')}"  >
                                                        <f:convertNumber pattern="#,###,##0.00"/>
                                                    </h:outputText>
                                                    <h:outputText  value="#{orcVer.valor}" rendered="#{orcVer.atoCampo.tipo.equalsIgnoreCase('String')}" /> 
                                                </td>
                                            </tr>
                                        </ui:repeat>
                                    </table>
                                </h:panelGrid>
                            </p:dialog>

                            <p:dialog  header="Informações do Selo" widgetVar="seloDlg" modal="true">  
                                <h:panelGrid columns="2" id="pesquisarProtocoloDlg" styleClass="textAlignRight" columnClasses="textAlignRight"> 
                                    <h:outputLabel value="Número: " style="font-weight: bold;"/>

                                    <p:row>
                                        <h:outputText value="#{managerVisualizarProtocolo.seloVer.numero}"/>
                                        <h:outputLabel value="Série: " style="font-weight: bold; float: none;"/>
                                        <h:outputText value="#{managerVisualizarProtocolo.seloVer.serie}"/>
                                    </p:row>
                                    <h:outputLabel value="Tipo de Selo " style="font-weight: bold;"/>
                                    <h:outputText value="#{managerVisualizarProtocolo.seloVer.tipoSelo.nome}"/>
                                    <h:outputLabel value="Data Entrada: " style="font-weight: bold;"/>
                                    <h:outputText value="#{managerVisualizarProtocolo.seloVer.detalheEntradaSelo.entradaSelo.dataEntrada}">
                                        <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss"/>
                                    </h:outputText>
                                    <h:outputLabel value="Fornecedor: " style="font-weight: bold;"/>
                                    <h:outputText value="#{managerVisualizarProtocolo.seloVer.detalheEntradaSelo.entradaSelo.fornecedor.nome}"/>
                                    <h:outputLabel value="Comentário: " style="font-weight: bold;"/>
                                    <h:outputText value="#{managerVisualizarProtocolo.seloVer.detalheEntradaSelo.entradaSelo.comentario}"/>
                                </h:panelGrid>
                                <f:facet name="footer">
                                    <center>
                                        <p:commandButton value="Fechar" icon="ui-icon-close" onclick="seloDlg.hide()"/>
                                    </center>
                                </f:facet>
                            </p:dialog>
                        </fieldset>
                        <br/>


                        <h:panelGrid columns="2" 
                                     id="panelObs" styleClass="textAlignRight" 
                                     style="width: 100%"> 

                            <p:dataTable var="parc" id="atoItemTable" 
                                         value="#{managerVisualizarProtocolo.protocolo.parcelas}"
                                         rowIndexVar="rowid" style="width:100%;" 
                                         rows="8"
                                         emptyMessage="Nenhum Parcela" 
                                         paginatorPosition="bottom" paginator="true"
                                         paginatorTemplate="{PageLinks}"  
                                         >  
                                <f:facet name="header">
                                    Parcelas
                                </f:facet>

                                <p:column style="width: 10px">  

                                    <p:graphicImage url="/media/img/verde_.png" rendered="#{!parc.ativo}" title="Parcela Paga" />

                                    <p:commandLink rendered="#{parc.ativo}" onclick="pagParcela.show()"
                                                   actionListener="#{managerVisualizarProtocolo.valorParcela(parc)}"
                                                   title="Pagar Parcela" update=":tabView:orcamento:pag" immediate="true" > 
                                        <p:graphicImage url="/media/img/verme_.png" rendered="#{parc.ativo}"/>
                                    </p:commandLink>    


                                </p:column>

                                <p:column headerText="Valor Pago" style="width:100px">  
                                    <h:outputText value="#{parc.valorPago}" style="float: right;" />  
                                </p:column>
                                <p:column headerText="Valor a  Pagar" style="width:100px">  
                                    <h:outputText value="#{parc.valorAPagar}" style="float: right;" />  
                                </p:column>

                                <p:column headerText="Data Pagamento" style="width:100px">  
                                    <h:outputText value="#{parc.dataPag}" rendered="#{parc.dataPag eq null ? false : true}" style="float: right;" >  
                                        <f:convertDateTime pattern="dd/MM/yyyy"/>
                                    </h:outputText>
                                    <h:outputText value="Pendente"  rendered="#{parc.dataPag eq null ? true : false}"/>
                                </p:column>


                            </p:dataTable> 

                            <h:panelGrid columns="2"  styleClass="textAlignRight" 
                                         columnClasses="textAlignRight"  >

                                <h:outputLabel value="Cartório: "/>
                                <p:row>
                                    <p:inputText id="totalCartorio" value="#{managerVisualizarProtocolo.protocolo.valorTotalCartorio}" readonly="true"  
                                                 style="width: 183px; text-align: right"
                                                 onchange="jQuery(this).autoNumeric({aSep: '.', aDec: ','} );" >
                                        <f:convertNumber pattern="#,###,##0.00"/>
                                    </p:inputText>

                                </p:row>
                                <h:outputLabel value="Fermojupi: " />
                                <p:row>
                                    <p:inputText id="totalFermojupi" readonly="true" 
                                                 style="width: 183px; text-align: right" 
                                                 value="#{managerVisualizarProtocolo.protocolo.valorTotalFermojupi}"
                                                 onchange="jQuery(this).autoNumeric({aSep: '.', aDec: ','} );">
                                        <f:convertNumber pattern="#,###,##0.00"/>
                                    </p:inputText>
                                </p:row>
                                <h:outputLabel value="Valor Total: "/>   
                                <p:row>
                                    <p:inputText id="totalGeral" readonly="true" 
                                                 value="#{managerVisualizarProtocolo.protocolo.valorTotalCartorio.add(managerVisualizarProtocolo.protocolo.valorTotalFermojupi)}"
                                                 style="width: 183px; color:#3B5998; font-weight: bold; text-align: right"
                                                 onchange="jQuery(this).autoNumeric({aSep: '.', aDec: ','} );">
                                        <f:convertNumber pattern="#,###,##0.00"/>
                                    </p:inputText>

                                </p:row>
                                <h:outputLabel value="Valor Recebido: "/>   
                                <p:row>
                                    <p:inputText  readonly="true" 
                                                  value="#{managerVisualizarProtocolo.recebidoParcela}"
                                                  style="width: 183px; color: #{managerVisualizarProtocolo.recebidoParcela.compareTo(managerVisualizarProtocolo.protocolo.valorTotalCartorio.add(managerVisualizarProtocolo.protocolo.valorTotalFermojupi)) eq 0 ? '#3B5998':'#CB2424'};
                                                  font-weight: bold; text-align: right"
                                                 onchange="jQuery(this).autoNumeric({aSep: '.', aDec: ','} );">
                                        <f:convertNumber pattern="#,###,##0.00"/>
                                    </p:inputText>

                                </p:row>
                            </h:panelGrid>

                        </h:panelGrid>

                        <p:dialog header="Pagar Parcela" resizable="false" modal="true" widgetVar="pagParcela">


                            <h:panelGrid columns="2" id="pag" styleClass="textAlignRight"
                                         columnClasses="textAlignRight" >

                                <h:outputLabel value="Valor: " style="width: 90px;"/>
                                <h:outputText value="R$ #{managerVisualizarProtocolo.sumTotal}" >
                                    <f:convertNumber pattern="#,###,##0.00"/>
                                </h:outputText>

                                <h:outputLabel value="Forma de Pag.: " style="width: 90px;"/>

                                <p:selectOneMenu value="#{managerVisualizarProtocolo.protocolo.formasPagamento}"  
                                                 label="Tipo " style="width: 183px" onchange="">  
                                    <f:selectItems value="#{managerProtocolo.selectItemFormPag()}"  />
                                    <p:ajax event="change"  update="pag" listener="#{managerVisualizarProtocolo.verificaPag()}"/>
                                </p:selectOneMenu> 

                                <h:outputLabel value="Recebido: " rendered="#{!managerVisualizarProtocolo.parc}" style="width: 83px;"/>
                                <p:row rendered="#{!managerVisualizarProtocolo.parc}">
                                    <p:inputText id="recebido" rendered="#{!managerVisualizarProtocolo.parc}" 
                                                 value="#{managerVisualizarProtocolo.recebido}" 
                                                 style="width: 183px; text-align: right" onkeypress="jQuery(this).autoNumeric({aSep: '.', aDec: ','} );">
                                        <p:ajax event="keyup" update="troco" listener="#{managerVisualizarProtocolo.caixa()}" />  
                                        <p:ajax event="change" update="troco" listener="#{managerVisualizarProtocolo.caixa()}" />  
                                        <f:convertNumber pattern="#,###,##0.00"/>
                                    </p:inputText> 
                                </p:row>
                                <h:outputLabel value="Troco: " rendered="#{!managerVisualizarProtocolo.parc}" />
                                <p:row rendered="#{!managerVisualizarProtocolo.parc}">
                                    <p:inputText rendered="#{!managerVisualizarProtocolo.parc}" value="#{managerVisualizarProtocolo.troco}" id="troco"
                                                 style="width: 183px; text-align: right" 
                                                 readonly="true"
                                                 onchange="jQuery(this).autoNumeric({aSep: '.', aDec: ','} );">
                                        <f:convertNumber pattern="#,###,##0.00"/>
                                    </p:inputText>
                                </p:row>

                                <h:outputLabel value="Quantidade. " style="width: 85px;" 
                                               rendered="#{managerVisualizarProtocolo.parc}"/>

                                <p:inputText id="qtd" rendered="#{managerVisualizarProtocolo.parc}" value="#{managerVisualizarProtocolo.qtdParc}" 
                                             style="width: 183px; text-align: right" />

                                <h:outputLabel value="Entrada: "  style="width: 83px;" rendered="#{managerVisualizarProtocolo.parc}"/>
                                <p:row id="rowPag" rendered="#{managerVisualizarProtocolo.parc}">
                                    <p:inputText rendered="#{managerVisualizarProtocolo.parc}" 
                                                 value="#{managerVisualizarProtocolo.entrada}" onkeypress="jQuery(this).autoNumeric({aSep: '.', aDec: ','} );"
                                                 style="width: 183px; text-align: right" 
                                                 >
                                        <f:convertNumber pattern="#,###,##0.00"/>
                                    </p:inputText> 
                                </p:row>
                            </h:panelGrid>
                            <f:facet name="footer">
                                <center>
                                    <p:commandButton value="Pagar" icon="ui-icon-check" 
                                                     actionListener="#{managerVisualizarProtocolo.pagarParcela()}"  update="@form"/>
                                    <p:commandButton value="Cancelar" icon="ui-icon-close"
                                                     onclick="pagParcela.hide()"/>
                                </center>
                            </f:facet>

                        </p:dialog>



                        <br/>
                        <h:outputLabel value="Observação: "/>
                        <br/>
                        <p:inputTextarea value="#{managerVisualizarProtocolo.protocolo.obs}" 
                                         cols="85" style="width: 98%;" rows="5"  />       


                        <div class="buttonAction">
                            
                            <p:commandButton icon="ui-icon-plusthick" value="Atualizar" actionListener="#{managerVisualizarProtocolo.atualizar()}" update="@form" 
                                             process="@form"/>
                            <p:commandButton icon="ui-icon-print" value="Recibo"
                                             actionListener="#{managerVisualizarProtocolo.relatorioProtocoloRecibo()}"
                                             ajax="false" update="@form" onclick="this.form.target='_blank'"
                                             process="@form"/>
                            <p:commandButton icon="ui-icon-print" value="Relatório" onclick="this.form.target='_blank'"
                                             actionListener="#{managerVisualizarProtocolo.relatorioProtocolo()}" ajax="false" update="@form" 
                                             process="@form"/>


                            
                            <center>
                                <p:ajaxStatus style="width:16px;height:16px;" >  
                                    <f:facet name="start">  
                                        <h:graphicImage value="/media/img/loading.gif" />  
                                    </f:facet>  
                                    <f:facet name="complete">  
                                        <h:outputText value="" />  
                                    </f:facet>  
                                </p:ajaxStatus>
                            </center>
                        </div>


                    </h:form>

                </p:tab>
            </p:tabView>
        </ui:define>
    </ui:composition>

</html>